package com.metadata_system.common.dto;

import lombok.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ApiResult {

    private String message = "";
    private int code = 0;
    @Builder.Default
    private Object data = new Object[]{};
    @Builder.Default
    private Object extendInfo = new Object[]{};
    private long timeStamp = 0;
    private long execTime = 0;

    {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
        }
        this.extendInfo = new Object[]{};
    }

    // 成功
    public static ApiResult success() {
        ApiResult apiResult = new ApiResult();
        apiResult.data = new Object[]{};
        apiResult.timeStamp = new Date().getTime();
        return apiResult;
    }

    // 分页-成功
    public static ApiResult successPage(List list, Integer total) {
        HashMap<String, Object> backMap = new HashMap<>();
        backMap.put("total", total);
        backMap.put("list", list);
        return ApiResult.success(backMap);
    }

    public static ApiResult successPage(List list, Integer total, Map map) {
        HashMap<String, Object> backMap = new HashMap<>();
        backMap.put("total", total);
        backMap.put("list", list);
        backMap.put("sumMap", map);
        return ApiResult.success(backMap);
    }

    public static ApiResult syncList(List list, Integer total, List deleteIdList) {
        HashMap<String, Object> backMap = new HashMap<>();
        backMap.put("total", total);
        backMap.put("list", list);
        backMap.put("deleteIdList", deleteIdList);
        return ApiResult.success(backMap);
    }

    // 成功
    public static ApiResult success(Object data) {
        ApiResult apiResult = new ApiResult();
        apiResult.data = data;
        apiResult.timeStamp = new Date().getTime();
        return apiResult;
    }


    // 错误-0
    public static ApiResult error(String message) {
        ApiResult apiResult = new ApiResult();
        apiResult.code = 1;
        apiResult.message = message;
        apiResult.timeStamp = new Date().getTime();
        return apiResult;
    }

    // 错误-1
    public static ApiResult error(String message, int code) {
        ApiResult apiResult = new ApiResult();
        apiResult.code = code;
        apiResult.message = message;
        apiResult.timeStamp = new Date().getTime();
        return apiResult;
    }

    public boolean isError() {
        return this.code > 0;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Object getExtendInfo() {
        return extendInfo;
    }

    public void setExtendInfo(Object extendInfo) {
        this.extendInfo = extendInfo;
    }

    public long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public long getExecTime() {
        return execTime;
    }

    public void setExecTime(long execTime) {
        this.execTime = execTime;
    }
}
